


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: CipherPlugin</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.serializer.plugin</a> ]
</div>

<h1>Coverage Summary for Class: CipherPlugin (at.schauer.gregor.vienna.serializer.plugin)</h1>

<table class="coverageStats">

<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">CipherPlugin</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (9/ 9)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    87%
  </span>
  <span class="absValue">
    (40/ 46)
  </span>
</td>
</tr>
  <tr>
    <td class="name">CipherPlugin$EncryptMode</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
  </tr>
<tr>
  <td class="name"><strong>total</strong></td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (10/ 10)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    87.2%
  </span>
  <span class="absValue">
    (41/ 47)
  </span>
</td>
</tr>
</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.serializer.plugin;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.exception.ViennaException;
<i>19</i>&nbsp;import org.bouncycastle.util.encoders.Base64;
<i>20</i>&nbsp;
<i>21</i>&nbsp;import javax.crypto.Cipher;
<i>22</i>&nbsp;import javax.crypto.spec.IvParameterSpec;
<i>23</i>&nbsp;import java.security.Key;
<i>24</i>&nbsp;import java.util.HashMap;
<i>25</i>&nbsp;import java.util.Map;
<i>26</i>&nbsp;
<i>27</i>&nbsp;/**
<i>28</i>&nbsp; * @author Gregor Schauer
<i>29</i>&nbsp; */
<i>30</i>&nbsp;public class CipherPlugin&lt;T&gt; extends AbstractSerializerPlugin&lt;T&gt; {
<b class="fc"><i>31</i>&nbsp;	public static enum EncryptMode {NONE, KEY, VALUE, KEY_VALUE}</b>
<i>32</i>&nbsp;
<i>33</i>&nbsp;	protected Key key;
<i>34</i>&nbsp;	protected byte[] initVector;
<i>35</i>&nbsp;	protected EncryptMode mode;
<i>36</i>&nbsp;	protected IvParameterSpec ips;
<i>37</i>&nbsp;	protected Cipher encryptCipher;
<i>38</i>&nbsp;	protected Cipher decryptCipher;
<i>39</i>&nbsp;
<b class="fc"><i>40</i>&nbsp;	protected CipherPlugin(Key key, byte[] initVector, EncryptMode mode) {</b>
<b class="fc"><i>41</i>&nbsp;		this.key = key;</b>
<b class="fc"><i>42</i>&nbsp;		this.initVector = initVector;</b>
<b class="fc"><i>43</i>&nbsp;		this.mode = mode;</b>
<i>44</i>&nbsp;
<b class="fc"><i>45</i>&nbsp;		this.ips = new IvParameterSpec(initVector);</b>
<b class="fc"><i>46</i>&nbsp;		this.encryptCipher = createEncryptCipher();</b>
<b class="fc"><i>47</i>&nbsp;		this.decryptCipher = createDecryptCipher();</b>
<b class="fc"><i>48</i>&nbsp;	}</b>
<i>49</i>&nbsp;
<i>50</i>&nbsp;	protected Cipher createEncryptCipher() {
<b class="fc"><i>51</i>&nbsp;		return createCipher(key, ips, Cipher.ENCRYPT_MODE);</b>
<i>52</i>&nbsp;	}
<i>53</i>&nbsp;
<i>54</i>&nbsp;	protected Cipher createDecryptCipher() {
<b class="fc"><i>55</i>&nbsp;		return createCipher(key, ips, Cipher.DECRYPT_MODE);</b>
<i>56</i>&nbsp;	}
<i>57</i>&nbsp;
<i>58</i>&nbsp;	protected static Cipher createCipher(Key key, IvParameterSpec ips, int mode) {
<i>59</i>&nbsp;		try {
<b class="fc"><i>60</i>&nbsp;			Cipher cipher = Cipher.getInstance(key.getAlgorithm());</b>
<b class="fc"><i>61</i>&nbsp;			cipher.init(mode, key, ips);</b>
<b class="fc"><i>62</i>&nbsp;			return cipher;</b>
<b class="nc"><i>63</i>&nbsp;		} catch (Exception e) {</b>
<b class="nc"><i>64</i>&nbsp;			throw new ViennaException(e);</b>
<i>65</i>&nbsp;		}
<i>66</i>&nbsp;	}
<i>67</i>&nbsp;
<i>68</i>&nbsp;	@Override
<i>69</i>&nbsp;	public Map&lt;String, Object&gt; preConstruct(Map&lt;String, Object&gt; map, Class&lt;? extends T&gt; clazz) {
<b class="fc"><i>70</i>&nbsp;		return process(map, decryptCipher);</b>
<i>71</i>&nbsp;	}
<i>72</i>&nbsp;
<i>73</i>&nbsp;	@Override
<i>74</i>&nbsp;	public Map&lt;String, Object&gt; postFinalize(Map&lt;String, Object&gt; map, T obj) {
<b class="fc"><i>75</i>&nbsp;		return process(map, encryptCipher);</b>
<i>76</i>&nbsp;	}
<i>77</i>&nbsp;
<i>78</i>&nbsp;	protected Map&lt;String, Object&gt; process(Map&lt;String, Object&gt; map, Cipher cipher) {
<b class="fc"><i>79</i>&nbsp;		if (mode == EncryptMode.NONE) {</b>
<b class="fc"><i>80</i>&nbsp;			return map;</b>
<i>81</i>&nbsp;		}
<b class="fc"><i>82</i>&nbsp;		boolean encr = cipher == encryptCipher;</b>
<b class="fc"><i>83</i>&nbsp;		Map&lt;String, Object&gt; result = new HashMap&lt;String, Object&gt;();</b>
<i>84</i>&nbsp;
<i>85</i>&nbsp;		try {
<b class="fc"><i>86</i>&nbsp;			if (mode == EncryptMode.KEY) {</b>
<b class="fc"><i>87</i>&nbsp;				for (Map.Entry&lt;String, Object&gt; entry : map.entrySet()) {</b>
<b class="fc"><i>88</i>&nbsp;					if (entry.getKey() == null) {</b>
<b class="nc"><i>89</i>&nbsp;						continue;</b>
<i>90</i>&nbsp;					}
<b class="fc"><i>91</i>&nbsp;					String newKey = decode(cipher.doFinal(encode(String.valueOf(entry.getKey()), encr)), encr);</b>
<b class="fc"><i>92</i>&nbsp;					result.put(newKey, entry.getValue());</b>
<b class="fc"><i>93</i>&nbsp;				}</b>
<b class="fc"><i>94</i>&nbsp;			} else if (mode == EncryptMode.VALUE) {</b>
<b class="fc"><i>95</i>&nbsp;				for (Map.Entry&lt;String, Object&gt; entry : map.entrySet()) {</b>
<b class="fc"><i>96</i>&nbsp;					if (entry.getValue() == null) {</b>
<b class="nc"><i>97</i>&nbsp;						continue;</b>
<i>98</i>&nbsp;					}
<b class="fc"><i>99</i>&nbsp;					String newValue = decode(cipher.doFinal(encode(String.valueOf(entry.getValue()), encr)), encr);</b>
<b class="fc"><i>100</i>&nbsp;					result.put(entry.getKey(), newValue);</b>
<b class="fc"><i>101</i>&nbsp;				}</b>
<b class="fc"><i>102</i>&nbsp;			} else if (mode == EncryptMode.KEY_VALUE) {</b>
<b class="fc"><i>103</i>&nbsp;				for (Map.Entry&lt;String, Object&gt; entry : map.entrySet()) {</b>
<b class="fc"><i>104</i>&nbsp;					String newKey = entry.getKey() == null ? entry.getKey() :</b>
<i>105</i>&nbsp;							decode(cipher.doFinal(encode(String.valueOf(entry.getKey()), encr)), encr);
<b class="fc"><i>106</i>&nbsp;					Object newValue = entry.getValue() == null ? entry.getValue() :</b>
<i>107</i>&nbsp;							decode(cipher.doFinal(encode(String.valueOf(entry.getValue()), encr)), encr);
<b class="fc"><i>108</i>&nbsp;					result.put(newKey, newValue);</b>
<b class="fc"><i>109</i>&nbsp;				}</b>
<i>110</i>&nbsp;			}
<b class="fc"><i>111</i>&nbsp;			return result;</b>
<b class="nc"><i>112</i>&nbsp;		} catch (Exception e) {</b>
<b class="nc"><i>113</i>&nbsp;			throw new ViennaException(e);</b>
<i>114</i>&nbsp;		}
<i>115</i>&nbsp;	}
<i>116</i>&nbsp;
<i>117</i>&nbsp;	protected static String decode(byte[] bytes, boolean encr) {
<b class="fc"><i>118</i>&nbsp;		return encr ? new String(Base64.encode(bytes)) : new String(bytes);</b>
<i>119</i>&nbsp;	}
<i>120</i>&nbsp;
<i>121</i>&nbsp;	protected static byte[] encode(String str, boolean encr) {
<b class="fc"><i>122</i>&nbsp;		return encr ? str.getBytes() : Base64.decode(str.getBytes());</b>
<i>123</i>&nbsp;	}
<i>124</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:23</div>
</div>
</body>
</html>
